#pragma once
#include <iostream>

typedef struct Graph {
    char* vertices;
    int** edges;
    int verNum;
    int edgesNum;
}Graph;

Graph* InitGraph(int verNum) {
    Graph* graph = (Graph*)malloc(sizeof(Graph));

    graph->vertices = (char*)malloc(sizeof(char) * verNum);
    graph->edges = (int**)malloc(sizeof(int) * verNum);

    for (int i = 0; i < verNum; i++) {
        graph->edges[i] = (int*)malloc(sizeof(int) * verNum);
    }

    graph->verNum = verNum;
    graph->edgesNum = 0;
    return graph;
}

/*
* 传入初始化的图、所有图的顶点、锁头边信息(邻接矩阵)
*/
void CreateGraph(Graph* graph, char* vertices, int* edge) {
    graph->vertices = vertices;
    for (int i = 0; i < graph->verNum; i++) {
        for (int j = 0; j < graph->verNum; j++) {
            graph->edges[i][j] = *(edge + i * graph->verNum + j);   //二维数组指针地址偏移, 再解引用
            if (graph->edges[i][j] != 0) {
                graph->edgesNum++;
            }
        }
    }

    graph->edgesNum /= 2;   //无向图边数为 1/2 的邻接矩阵
}
